package com.feige.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.feige.pojo.Permission;
import com.feige.pojo.vo.PermissionVo;
import com.feige.result.AjaxResult;
import com.feige.service.PermissionService;
import com.feige.service.RolePermissionService;
import com.feige.utils.common.BeanUtils;
import com.feige.utils.string.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 权限管理 前端控制器
 * </p>
 *
 * @author feige
 * @since 2020-07-20
 */
@Api(tags = "权限控制类")
@RestController
@RequestMapping("/admin/permission")
public class PermissionController {

    @Resource
    private PermissionService permissionService;

    @Resource
    private RolePermissionService rolePermissionService;

    @PreAuthorize("@ss.hasPerm('user:menu:list')")
    @ApiOperation(value = "获取所有菜单,转化为树形结构")
    @GetMapping("/get-tree-all")
    public AjaxResult queryAllMenu(){
        List<Permission> permissions = permissionService.queryAllMenu();
        return AjaxResult.success(permissions);
    }

    @PreAuthorize("@ss.hasPerm('user:menu:list')")
    @ApiOperation(value = "获取所有菜单")
    @GetMapping("/get-list")
    public AjaxResult getAll(String name,Integer status){
        QueryWrapper<Permission> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(name)){
            wrapper.like("name",name);
        }
        if (status != null){
            wrapper.eq("status",status);
        }
        List<Permission> permissions = permissionService.list(wrapper);
        List<PermissionVo> permissionVos = BeanUtils.convertList2List(permissions, PermissionVo.class);
        return AjaxResult.success(permissionVos);
    }

    @PreAuthorize("@ss.hasPerm('user:menu:list')")
    @ApiOperation(value = "根据角色ID获取对应的权限")
    @GetMapping("/get/{roleId}")
    public AjaxResult selectPermissionByRoleId(@PathVariable Long roleId){
        List<Long> permissionIds = rolePermissionService.getPermissionId(roleId);
        return AjaxResult.success(permissionIds );
    }

    @PreAuthorize("@ss.hasPerm('user:menu:list')")
    @ApiOperation(value = "根据权限ID获取对应的权限")
    @GetMapping("/get-menu/{id}")
    public AjaxResult getPermissionById(@PathVariable Long id){
        Permission permission = permissionService.getById(id);
        return AjaxResult.success(permission);
    }

    @PreAuthorize("@ss.hasPerm('user:menu:add')")
    @ApiOperation(value = "新增菜单")
    @PostMapping("/save")
    public AjaxResult save(@Validated @RequestBody Permission permission){
        boolean save = permissionService.save(permission);
        if (save){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }

    @PreAuthorize("@ss.hasPerm('user:menu:edit')")
    @ApiOperation(value = "修改菜单")
    @PutMapping("/update")
    public AjaxResult update(@Validated @RequestBody Permission permission){
        boolean update = permissionService.updateById(permission);
        if (update){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }

    @PreAuthorize("@ss.hasPerm('user:menu:delete')")
    @ApiOperation(value = "级联删除菜单")
    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable Long id){
        boolean delete = permissionService.removeById(id);
        if (delete){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }

}

